import { _decorator, Component, Node } from 'cc';
import { BaseManager } from '../../libs/rxjs/cc3/BaseManager';
import { director } from 'cc';
import { color } from 'cc';
import { System } from 'cc';
import { jsb_sdk_m } from './JsbSDKManager';
import { DevConfig } from '../DevConfig';
const { ccclass, property } = _decorator;

@ccclass('ThinkingCocosSDKManager')
export class ThinkingCocosSDKManager extends BaseManager {

    public static instance: ThinkingCocosSDKManager;
    static ID = 'thinking_sdk_system';
    get logColorOverride() { return color().fromHEX('#66EE33') };

    static registerSystem() {
        director.unregisterSystem(director.getSystem(ThinkingCocosSDKManager.ID));
        director.registerSystem(ThinkingCocosSDKManager.ID, thinking_sdk_m, System.Priority.MEDIUM);
        thinking_sdk_m.init();
    }

    onInitManager() {
        this.log('onInitManager', this.timestamp);
    }

    get enableLog() { return true; }

    requestLogin(uid: string) {
        this.enableLog && this.log('requestLogin', uid);
        if (!DevConfig.DATA_REPORT) return;
        jsb_sdk_m.sendMsg('ThinkingCocosSDK.login', JSON.stringify({ uid }));
    }

    // 公共属性
    requestSetSuperProperties(data: Record<string, any>) {
        this.enableLog && this.log('requestSetSuperProperties', data);
        if (!DevConfig.DATA_REPORT) return;
        jsb_sdk_m.sendMsg('ThinkingCocosSDK.setSuperProperties', JSON.stringify(data));
    }

    requestTrack(eventName: string, data: Record<string, any>) {
        this.enableLog && this.log('requestTrack', eventName, data);
        if (!DevConfig.DATA_REPORT) return;
        data['_track_event_name'] = eventName;
        jsb_sdk_m.sendMsg('ThinkingCocosSDK.track', JSON.stringify(data));
    }

    // 带User 的都是用户属性
    requestUserSet(data: Record<string, any>) {
        this.enableLog && this.log('requestUserSet', data);
        if (!DevConfig.DATA_REPORT) return;
        jsb_sdk_m.sendMsg('ThinkingCocosSDK.userSet', JSON.stringify(data));
    }

    requestUserSetOnce(data: Record<string, any>) {
        this.enableLog && this.log('requestUserSetOnce', data);
        if (!DevConfig.DATA_REPORT) return;
        jsb_sdk_m.sendMsg('ThinkingCocosSDK.userSetOnce', JSON.stringify(data));
    }

    requestUserAdd(data: Record<string, any>) {
        this.enableLog && this.log('requestUserAdd', data);
        if (!DevConfig.DATA_REPORT) return;
        jsb_sdk_m.sendMsg('ThinkingCocosSDK.userAdd', JSON.stringify(data));
    }

    requestGetDeviceId() {
        this.enableLog && this.log('requestGetDeviceId')
        if (!DevConfig.DATA_REPORT) return;
        jsb_sdk_m.sendMsg('ThinkingCocosSDK.getDeviceId', "{}");
    }

}

globalThis.ThinkingCocosSDKManager = ThinkingCocosSDKManager;

export const thinking_sdk_m = ThinkingCocosSDKManager.instance = new ThinkingCocosSDKManager();